package com.boss.salary.service.impl;

import com.boss.common.entity.WfActionDTO;
import com.boss.common.enums.StatusCodeEnum;
import com.boss.common.service.ICommonService;
import com.boss.salary.dao.IAgencyInfoAuditDao;
import com.boss.salary.dao.IAgencyVerifyDao;
import com.boss.salary.entity.AgencyVerify;
import com.boss.salary.entity.HomesMenuTree;
import com.boss.salary.entity.SalaryBackLog;
import com.boss.salary.service.IHomeMenuTreeService;
import com.boss.salary.service.ISalary2OutsideService;
import com.boss.salary.service.ISalaryBackLogService;
import com.boss.salary.workflow.service.ISalaryWorkFlowService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hao
 * @date 2023/4/11 15:14
 */
@Service
public class Salary2OutsideServiceImpl implements ISalary2OutsideService {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(Salary2OutsideServiceImpl.class);
    @Autowired
    private IHomeMenuTreeService homeMenuTreeService;
    @Autowired
    private ISalaryWorkFlowService iSalaryWorkFlowService;

    @Autowired
    private IAgencyInfoAuditDao iAgencyInfoAuditDao;


    
    @Autowired
    private IAgencyVerifyDao iAgencyVerifyDao;

    @Autowired
    private ICommonService iCommonService;


    @Autowired
    private ISalaryBackLogService salaryBackLogService;




    @Override
    public List<Map<String,Object>> getFlowTaskData(UserDTO userDTO, String tokenid, List<Map<String,Object>> config, String userguid, String roleguid, String province, String year) {

        List<Map<String,Object>> result = new ArrayList<>();
        //定义一个单位bizkey对应单位code+name的map
        Map<String, String> avMap = new HashMap<>();
        //获取菜单
        List<HomesMenuTree> menuTree = homeMenuTreeService.getMenuTree(tokenid);
        List<HomesMenuTree> menuTreeHaschile =new ArrayList<>();
//        logger.info("平台待办获取到的菜单信息menuTree：{}",menuTree);
        // 递归取出map中的map
        for(HomesMenuTree ma:menuTree) {
            fMap(menuTreeHaschile,ma);
        }
//        logger.info("平台待办获取到的菜单信息menuTreeHaschile：{}",menuTreeHaschile);

        //获取全部单位信息
        List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, "0",tokenid);
//        logger.info("平台待办获取到的单位信息agencyIds：{}",agencyIds);
        //获取所有代编单位状态
        List<AgencyVerify> avs = iAgencyVerifyDao.getSalaryDataByAgencyId(childrenId);
        for (AgencyVerify av : avs) {
            //把所有单位信息放到map里方便后面取值
            avMap.put(av.getBizKey(), av.getAgencyCode() + av.getAgencyName());
        }

        //查询当前单位AgencyVerify表，判断待办事项是否为本单位待办用
        if (!StringUtils.isEmpty(menuTreeHaschile) && menuTreeHaschile.size() > 0) {
            Map<String,Object> auditMap = null;
            for (HomesMenuTree map : menuTreeHaschile) {
                Map m = new HashMap();
                List<Map<String,Object>> tododata=new ArrayList<>();
                m.put("menuguid", map.getGuid() + "#" + roleguid);
                // 单位审核
                if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("unitAudit")!=-1) {
                    Map<String,Object> queryParam = new HashMap<>(1);
                    queryParam.put("flag", "1");
                    int total = iAgencyInfoAuditDao.queryAgencyInfo(queryParam, childrenId);
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }

                }// 工资单位编报*/
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("salaryEdit")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid());
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);

                    }
                }
                // 工资单位审核
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("salaryAudit")!=-1) {
                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        int total = 0;
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeybacks = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    if (!StringUtils.isEmpty(bizKeybacks) && bizKeybacks.size() > 0) {
                        int total = 0;
                        for (String bizKey : bizKeybacks) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "工资数据被退回");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "4");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }
                } else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("salaryAuditMiddle")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        int total = 0;
                        for (String bizKey : bizKeys) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }
                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bizKeybacks = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    if (!StringUtils.isEmpty(bizKeybacks) && bizKeybacks.size() > 0) {
                        int total = 0;
                        for (String bizKey : bizKeybacks) {
                            if (StringUtils.isEmpty(avMap.get(bizKey))) {
                                continue;
                            }
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "工资数据被退回");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "4");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }
                }
                // 工资财政确认
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("salaryConfirm")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO_BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                       auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }

                }
                // 初审
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("firstAudit")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }

                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bacbizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int bactotal = 0;
                    if (!StringUtils.isEmpty(bacbizKeys) && bacbizKeys.size() > 0) {
                        for (String bizKey : bacbizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                bactotal++;
                            }
                        }
                    }
                    if (bactotal > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", bactotal);
                        auditMap.put("statusName", "工资数据被退回");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "4");
                        auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }



                }
                // 复审
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("secondAudit")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                       auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }

                    wfActionDTO.setStatus(StatusCodeEnum.BESENDBACK.getCode());
                    List<String> bacbizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int bactotal = 0;
                    if (!StringUtils.isEmpty(bacbizKeys) && bacbizKeys.size() > 0) {
                        for (String bizKey : bacbizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                bactotal++;
                            }
                        }
                    }
                    if (bactotal > 0) {
                        auditMap = new HashMap<>(2);
                        auditMap.put("count", bactotal);
                        auditMap.put("statusName", "工资数据被退回");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "4");
                       auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }

                }
                // 终审
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("finalAudit")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO_BESENDBACK.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                       auditMap.put("menuname", map.getF_FullName());
                       tododata.add(auditMap);
                    }

                }
                //生成工资数据
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("salaryGenerate")!=-1) {

                    WfActionDTO wfActionDTO = new WfActionDTO();
                    // 菜单id
                    wfActionDTO.setMenuguid(map.getGuid() );
                    wfActionDTO.setStatus(StatusCodeEnum.TODO.getCode());
                    List<String> bizKeys = iSalaryWorkFlowService.queryWorkflowInfo(userDTO, wfActionDTO,tokenid);
                    int total = 0;
                    if (!StringUtils.isEmpty(bizKeys) && bizKeys.size() > 0) {
                        for (String bizKey : bizKeys) {
                            if (!StringUtils.isEmpty(avMap.get(bizKey))) {
                                total++;
                            }
                        }
                    }
                    if (total > 0) {
                        auditMap = new HashMap<>();
                        auditMap.put("count", total);
                        auditMap.put("statusName", "待办");
                        auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                        auditMap.put("status", "1");
                       auditMap.put("menuname", map.getF_FullName());
                        tododata.add(auditMap);
                    }
                }//生成银行数据
                else if (!StringUtils.isEmpty(map.getParam5()) && map.getParam5().indexOf("bankCreate")!=-1) {
                    // 日志类型 bankback 生成银行回盘,payback支付回盘，bankerror生成银行错误回盘
                    List<SalaryBackLog> bankback = salaryBackLogService.getSalaryBackLogByProvince(userDTO.getProvince());
                    if (!CollectionUtils.isEmpty(bankback) && bankback.size() > 0) {
                        for (SalaryBackLog back : bankback) {
                            if("payback".equals(back.getLog_type())) {
                                auditMap = new HashMap<>();
                                auditMap.put("count", back);
                                auditMap.put("statusName", "待办");
                                auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                                auditMap.put("status", "1");
                                auditMap.put("menuname", map.getF_FullName());
                                tododata.add(auditMap);
                            }else if("bankerror".equals(back.getLog_type())) {
                                auditMap = new HashMap<>();
                                auditMap.put("count", back);
                                auditMap.put("statusName", "工资数据被退回");
                                auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                                auditMap.put("status", "4");
                                auditMap.put("menuname", map.getF_FullName());
                                tododata.add(auditMap);
                            }else if("paymentSuccess".equals(back.getLog_type())) {
                                auditMap = new HashMap<>();
                                auditMap.put("count", back);
                                auditMap.put("statusName", "待办");
                                auditMap.put("menuguid", map.getGuid() + "#" + roleguid);
                                auditMap.put("status", "1");
                                auditMap.put("menuname", map.getF_FullName());
                                tododata.add(auditMap);
                            }
                        }
                    }
                }

                m.put("tododata",tododata);
                if(!CollectionUtils.isEmpty(tododata)) {
                    result.add(m);
                }
            }
        }
        return result;
    }

    public void fMap(List<HomesMenuTree> mas ,HomesMenuTree ma) {
        if(!CollectionUtils.isEmpty(ma.getChildren())){
            for(HomesMenuTree cma:ma.getChildren()){
                fMap(mas,cma);
            }
//            mas.add(ma);
        }else{
            mas.add(ma);
        }
    }
}